package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.dao.SysAuditDao;
import com.ccp.dev.system.model.SysAudit;
import com.ccp.dev.system.model.SysRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 系统审计,操作日志 信息操作服务类
 *
 * @author zw
 * @date 2019-08-05 19:43:22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysAuditService extends BaseService<SysAudit> {

    @Resource
    private SysAuditDao sysAuditDao;
    @Resource
    private SysRoleService sysRoleService;

    public List<SysAudit> queryList(QueryFilter queryFilter,String logLeave) {
        //超管查询所有
        if (ContextUtil.isSuperAdmin()) {
            return super.queryList(queryFilter);
        }
        String currUserId = ContextUtil.getCurrentUserId();
        List<SysRole> allRole = sysRoleService.queryByUserId(currUserId);
        //是否包含三员角色
        boolean isContainThreeRole = sysRoleService.isContainThreeRole(currUserId);
        //普通用户查询数据
        if(!isContainThreeRole){
            if("0".equals(logLeave)){//系统日志
                return this.sysAuditDao.queryGeneralOperationLog(queryFilter);
            }else{//三员日志
                return new ArrayList<>();
            }
        }else{
            //安全保密管理员
            List<SysRole> aqbmgly = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_AQBMGLY)).collect(Collectors.toList());
            if(BeanUtils.isNotEmpty(aqbmgly)){
                if("0".equals(logLeave)){//系统日志
                    return this.sysAuditDao.queryGeneralOperationLog(queryFilter);
                }else{//三员日志
                    queryFilter.put("roleId",aqbmgly.get(0).getRoleId());
                    return this.sysAuditDao.queryListByAqbmgly(queryFilter);
                }

            }
            //安全审计员
            List<SysRole> aqsjy = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_AQSJY)).collect(Collectors.toList());
            if(BeanUtils.isNotEmpty(aqsjy)){
                if("0".equals(logLeave)){
                    return this.sysAuditDao.queryGeneralOperationLog(queryFilter);
                }else{
                    queryFilter.put("roleId",aqsjy.get(0).getRoleId());
                    return this.sysAuditDao.queryListByAqsjy(queryFilter);
                }
            }
            return new ArrayList<>();
        }
    }

    public List<SysAudit> queryLogToEchart(String logLeave){
        //超管查询所有
        if (ContextUtil.isSuperAdmin()) {
            return this.sysAuditDao.queryLogToEchartAdmin();
        }
        String currUserId = ContextUtil.getCurrentUserId();
        List<SysRole> allRole = sysRoleService.queryByUserId(currUserId);
        //是否包含三员角色
        boolean isContainThreeRole = sysRoleService.isContainThreeRole(currUserId);
        Map<String,Object> map = new HashMap<>();
        if(!isContainThreeRole){//普通用户
            if("0".equals(logLeave)){//系统日志
                return this.sysAuditDao.queryLogToEchart();
            }else{//三员日志
                return new ArrayList<>();
            }
        }else{//三员用户
            //安全保密管理员
            List<SysRole> aqbmgly = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_AQBMGLY)).collect(Collectors.toList());
            if(BeanUtils.isNotEmpty(aqbmgly)){
                //系统日志
                if("0".equals(logLeave)){
                    return this.sysAuditDao.queryLogToEchart();
                }else{//三员日志
                    map.put("roleId",aqbmgly.get(0).getRoleId());
                    return this.sysAuditDao.queryLogToEchartThreeRole(map);
                }

            }
            //安全审计员
            List<SysRole> aqsjy = allRole.stream().filter(a -> a.getRoleType().equals(SysRole.DEFAULT_AQSJY)).collect(Collectors.toList());
            if(BeanUtils.isNotEmpty(aqsjy)){
                if("1".equals(logLeave)){
                    map.put("roleId",aqsjy.get(0).getRoleId());
                    return this.sysAuditDao.queryLogToEchartThreeRole(map);
                }else{
                    return this.sysAuditDao.queryLogToEchart();
                }
            }
            return new ArrayList<>();
        }

    }

}
